//
//  SeventeenViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "SeventeenViewController.h"

@interface SeventeenViewController ()
@property(nonatomic,strong) CAShapeLayer *shapeLayer;
@end

@implementation SeventeenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"绘图曲线";
    [self xxxx];
}

-(void)xxxx{
/**绘制路径*/
/**利用贝塞尔曲线绘制折线*/
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    /**移动到当前点*/
    [bezierPath moveToPoint:CPM(50, 250)];
    [bezierPath addLineToPoint:CPM(110, 200)];
    [bezierPath addLineToPoint:CPM(220, 250)];
    [bezierPath addLineToPoint:CPM(300, 200)];
    
    _shapeLayer = [CAShapeLayer layer];
    /**设置路径*/
    _shapeLayer.path = bezierPath.CGPath;
    /**设置线条颜色*/
    _shapeLayer.strokeColor = [UIColor redColor].CGColor;
    /**设置填充颜色*/
    _shapeLayer.fillColor = [UIColor clearColor].CGColor;
    /**设置线宽*/
    _shapeLayer.lineWidth = 3.0;
    _shapeLayer.strokeEnd = 0.5;
    [self.view.layer addSublayer:_shapeLayer];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self startShapeLayerAnimation];
}

-(void)startShapeLayerAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 0.5;
//    animation.fromValue =@(0.0);
    animation.toValue = @(1.0);
    /**重复次数*/
    animation.repeatCount = HUGE_VALF;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    [_shapeLayer addAnimation:animation forKey:@"endStroke"];
}

-(void)drowRect:(CGRect)rect{
/**获取图形上下文*/
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    /**设置线条宽度*/
    CGContextSetLineWidth(ctx, 3.0);
    /**设置线条颜色*/
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    /**设置填充颜色*/
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    /**保存一份当前纯净的绘图上下文*/
    CGContextSaveGState(ctx);
    
    /**绘制直线*/
    const CGPoint point1[] = {CGPointMake(20, 70),CGPointMake(340, 70)};
    /**设置线头的样式*/
    CGContextSetLineCap(ctx, kCGLineCapRound);
    /**渲染*/
    CGContextStrokeLineSegments(ctx, point1, 2);
    
    /**绘制三角形*/
    /**设置绘制的起点*/
    CGContextMoveToPoint(ctx, 20, 100);
    CGContextAddLineToPoint(ctx, 20, 300);
    CGContextAddLineToPoint(ctx, 300, 300);
    /**闭合路径*/
    CGContextClosePath(ctx);
    /**渲染填充*/
    CGContextFillPath(ctx);
    /**渲染线条*/
    CGContextStrokePath(ctx);
    
    /**绘制虚线*/
    /**设置虚线实现和空白的长度*/
    const CGFloat lenght[] = {10,30};
    /**设置虚线模式*/
    CGContextSetLineDash(ctx, 8, lenght, 2);
    /**设置虚线的起点和终点*/
    CGContextMoveToPoint(ctx, 20, 330);
    CGContextAddLineToPoint(ctx, 340, 330);
    /**渲染虚线*/
    CGContextStrokePath(ctx);
    /**还原初始的绘图上下文*/
    CGContextRestoreGState(ctx);
    
    /**绘制矩形填充*/
//    CGContextFillRect(ctx, CRM(20, 360, 200, 150));
    /**绘制矩形线条*/
    CGContextStrokeRect(ctx, CRM(20, 360, 200, 150));
    
    /**绘制椭圆*/
    CGContextStrokeEllipseInRect(ctx, CRM(20, 530, 200, 130));
}
- (void)drawRect:(CGRect)rect {
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //设置线条宽度
    CGContextSetLineWidth(ctx, 3.0);
    //设置线条颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    //设置填充颜色
    CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
    
    //绘制曲线
    //设置绘制起始点
    CGContextMoveToPoint(ctx, 20, 200);
    //二次曲线
    //    CGContextAddQuadCurveToPoint(ctx, 170, 0, 340, 200);
    //贝塞尔曲线
    CGContextAddCurveToPoint(ctx, 30, 100, 100, 300, 340, 200);
    //渲染
    CGContextStrokePath(ctx);
    
    //绘制圆弧第二个参数：圆心；第三个参数：半径；第三四个参数：其实角度；最后一个参数：方向 0:顺时针，1：逆时针
    CGContextAddArc(ctx, 186, 300, 150, 0,M_PI, 0);
    //渲染
    //    CGContextStrokePath(ctx);
    CGContextFillPath(ctx);
    
}
@end
